# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql
from CatEye.settings import *

class CateyePipeline:
    def process_item(self, item, spider):
        print(item['name'],item['star'],item['time'])
        return item

class CateyeMysqlPipeline:
    def open_spider(self,spider):
        self.db=pymysql.connect(
                host='localhost'
                user='localuser'
                password='1c2c3t4v'
                database='db2'
                charset='utf8'
        )
        self.cursor=self.db.cursor()
    def process_item(self,item,spider):
        cmd='insert into movieinfo values(%s,%s,%s)'
        L=[item['name'],item['star'],item['time']]
        self.cursor.execute(cmd,L)
        self.db.commit()
        return item
    def close_spider(self,spider):
        self.cursor.close()
        self.db.close()
        print('Item executed.')
